package org.fatmansoft.teach.repository;

import org.fatmansoft.teach.models.StudentLog;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.time.LocalDateTime;
import java.util.List;


public interface StudentLogRepository extends JpaRepository<StudentLog,Integer> {
    //用于主键自增
    @Query(value = "SELECT MAX(studentLogId) FROM StudentLog")
    Integer getMaxId();

    //根据studentId查找该学生的所有日志信息
    List<StudentLog> findLogListByStudentStudentId(Integer studentId);
    @Query("SELECT s FROM StudentLog s " +
            "WHERE (:studentId IS NULL OR s.student.id = :studentId) " +
            "AND (:startDate IS NULL OR s.date >= :startDate) " +
            "AND (:endDate IS NULL OR s.date <= :endDate)"+
            "AND (:logType IS NULL OR s.log_type = :logType)"+
            "AND (s.code = :code)")
    List<StudentLog> findByStudentIdAndDateStringBetweenAndLogTypeAndCode(
            @Param("studentId") Integer studentId,
            @Param("startDate") String startDate,
            @Param("endDate") String endDate,
            @Param("logType") Integer logType,
            @Param("code") Integer code);

    void deleteByStudentLogId(Integer studentLogId);
}
